package VLSys;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import VLSObjects.*;

public class ManagementVehicleAdd extends JPanel{
    
    JComboBox manufacturer;
    JTextField model;
    JComboBox size;
    String[] manuchoice = { "", "Audi", "BMW", "Chevrolet", "Chery", "Chrysler", "Ferrari", "Ford", "Honda", "Hyundai", "Isuzu", "Jaguar", "Lamborghini", "Land Rover", "Lexus","Mazda","Mercedes-Benz","Mitsubishi","Nissan","Pilipinas Hino","Porsche","SsangYong","Subaru","Suzuki","Toyota","Volvo","Others"};
    String[] sizechoice = { "", "Small", "Medium","Large"};
    JButton submitButton;
    JButton cancelButton;
    JLabel vehicleManufacturerLabel;
    JLabel vehicleModelLabel;
    JLabel vehicleSizeLabel;
    JLabel addVehicleLabel;
    
    /*
    ImageIcon ask = new ImageIcon("images/prompt_ask.png");
    ImageIcon notice = new ImageIcon("images/prompt_notice.png");
    */
    
    ImageIcon notice_green = new ImageIcon("images/prompt_notice_green.png");
    ImageIcon ask_red = new ImageIcon("images/prompt_ask_red.png");
    ImageIcon notice_red = new ImageIcon("images/prompt_notice_red.png");
    
    String blank = "";
    String string[] = new String[5];
    
    
    public ManagementVehicleAdd(final JFrame frame)
    {
        
        setVisible(false);
        setLayout(null);
        setBackground(null);
        
               
        manufacturer = new JComboBox(manuchoice);
        model = new JTextField();
        size = new JComboBox(sizechoice);
        submitButton = new JButton();
        cancelButton = new JButton();
        vehicleManufacturerLabel = new JLabel("Manufacturer:");
        vehicleModelLabel = new JLabel("Model:");
        vehicleSizeLabel = new JLabel("Size:");
        
        addVehicleLabel = new JLabel("ADD VEHICLE");
        
        addVehicleLabel.setFont(new Font("sansserif", Font.PLAIN, 40));
        
        addVehicleLabel.setBounds(10,10,300,30);
        
        vehicleManufacturerLabel.setBounds(39,50,150,20);
        vehicleModelLabel.setBounds(82,75,150,20);
        vehicleSizeLabel.setBounds(92,100,150,20);
       
                
        manufacturer.setBounds(130,50,200,20);
        manufacturer.setBorder(null);
        model.setBounds(130,75,200,20);
        model.setBorder(null);
        size.setBounds(130,100,200,20);
        size.setBorder(null);
        
        
        submitButton.setText("Submit");
        submitButton.setBounds(175,125,75,20);
        cancelButton.setText("Clear");
        cancelButton.setBounds(255,125,75,20);
        
        
        add(manufacturer);
        add(model);
        add(size);
        add(submitButton);
        add(cancelButton);
        add(vehicleManufacturerLabel);
        add(vehicleModelLabel);
        add(vehicleSizeLabel);
        add(addVehicleLabel);
        
        model.addKeyListener(new KeyAdapter() {
          public void keyReleased(KeyEvent e) {
              
              if (e.getKeyCode()==39 || e.getKeyCode()==37)
        {
               
        }
              else 
                   model.setText(capitalizeFirstLetters(model.getText()));
          }
          public void keyTyped(KeyEvent e) {
              
          }

          public void keyPressed(KeyEvent e) {
              
          }
        });
        
        
        
        submitButton.addActionListener(new  ActionListener()
       {
            public void actionPerformed(ActionEvent e)
            {
                
                string[0] = (String)manufacturer.getSelectedItem(); //manufac
                string[1] = model.getText();                        //model
                string[2] = (String)size.getSelectedItem();         //size
                
                
                if(string[0].length() == 0 && string[1] == blank && string[2].length() == 0 )
                {
                    checkEmpty();
                    JOptionPane.showMessageDialog(frame,
                                        "Incomplete Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);
                    
                } 
                else if(string[1].length()== 0 || string[0] == blank || string[2] == blank )
                {
                    checkEmpty();
                    
                        if(string[1].length() > 25)
                        {
                            checkEmpty();
                            JOptionPane.showMessageDialog(frame,
                                                "Invalid and Incomplete Input",
                                                "Vehicle Logging Service",
                                                JOptionPane.WARNING_MESSAGE,
                                                notice_red);

                        }    
                   
                        else if(string[1].length() == 0 || string[0] == blank || string[2] == blank)
                        JOptionPane.showMessageDialog(frame,
                                            "Incomplete Input",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_red);
                     
                     
                    
                }   
                else if(string[1].length() > 25)
                {
                    checkEmpty();
                    JOptionPane.showMessageDialog(frame,
                                        "Invalid Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);

                     
                }            
                else if(string[1].length() <= 25 && string[0] != blank && string[2] != blank)   
                {
                    int n = JOptionPane.showConfirmDialog(
                            frame,
                            "Proceed with current values?",
                            "Vehicle Logging Service",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                            ask_red);
                    if(n==0)
                    {
                        Vehicle newVehicle = new Vehicle();
                        VehicleManager newVm = new VehicleManager();

                        
                        newVehicle.setManufacturer(string[0]);
                        newVehicle.setModel(string[1]);
                        newVehicle.setSize(string[2]);
                        
                       /*boolean existing = newVm.checkVehicle(string[0], string[1], "--");
                       //check if a similar record already exists
                           
                       if(existing == true) 
                           {    JOptionPane.showMessageDialog(frame,
                                            "Vehicle record already exists!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice);
                           }
                           else
                           {   //add to database if no similar record is found
                            */   boolean result = newVm.addVehicle(newVehicle);
                               if (result == true)
                               {     JOptionPane.showMessageDialog(frame,
                                            "Vehicle Added!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_green);
                               resetValues();
                               }
                           //}
                        }
                       else
                            {  System.out.print("ayaw!");

                            }
                }
            }
        }
        );
        
        
        cancelButton.addActionListener(new  ActionListener()
       {
            public void actionPerformed(ActionEvent e)
            {
               int n = JOptionPane.showConfirmDialog(
                            frame,
                            "Are you sure you want to clear the values?",
                            "Vehicle Logging Service",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                            ask_red);
               if(n==0)
                    { 
                resetValues();

                    }
            }
        }
        );
        
    }
    
    public void checkEmpty()
    {   
        String s = "";
       
        if((String)manufacturer.getSelectedItem() == s)
            manufacturer.setBackground(Color.YELLOW);
        
        if((String)manufacturer.getSelectedItem() != s)
            manufacturer.setBackground(null);
        
        if(model.getText().length() == 0)
            model.setBackground(Color.YELLOW);
               
        else if(model.getText().length() > 25)
            model.setBackground(Color.RED);
        
        else if(model.getText().length() <= 25)
            model.setBackground(null);
        
        if((String)size.getSelectedItem() == s)
            size.setBackground(Color.YELLOW);
        
                
        if((String)size.getSelectedItem() != s)
            size.setBackground(null);
        
    }
    
    
     public void resetValues()
        {

                    manufacturer.setBackground(null);
                    model.setBackground(null);
                    size.setBackground(null);
                    
                    manufacturer.setSelectedItem("");
                    model.setText("");
                    size.setSelectedItem("");


        }
     
     public static String capitalizeFirstLetters ( String s ) {

        for (int i = 0; i < s.length(); i++) {

            if (i == 0) {
                // Capitalize the first letter of the string.
                s = String.format( "%s%s", Character.toUpperCase(s.charAt(0)), s.substring(1) );
            }

            // Is this character a non-letter or non-digit?  If so
            // then this is probably a word boundary so let's capitalize
            // the next character in the sequence.
            if (!Character.isLetterOrDigit(s.charAt(i))) {
                if (i + 1 < s.length()) {
                    s = String.format( "%s%s%s",
                                 s.subSequence(0, i+1),
                                 Character.toUpperCase(s.charAt(i + 1)),
                                 s.substring(i+2) );
                }
            }

        }

        return s;

    }
    
}
